<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/xadmin/css/font.css">
    <link rel="stylesheet" href="/static/xadmin/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/apublic.css">
    <script src="/static/xadmin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/xadmin/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="/static/xadmin/html5.min.js"></script>
    <script src="/static/xadmin/respond.min.js"></script>
    <![endif]-->
    <script src="/static/until.js" charset="utf-8"></script>
    <script>
        if(!sessionStorage.getItem('token')) window.top.location.href='/login.html';
    </script>
    <style>
        .nop{padding:0px 5px!important;margin-left:2px!important;}
    </style>
</head>
<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid check-layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline">
                            <input type="text" name="keyword" placeholder="标题" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">状态</option>
                                <option value="1">编辑</option>
                                <option value="2">发布</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn" href="javascript:void(0);" onclick="adduser()"><i class="layui-icon">&#xe654;</i>添加商品</a>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn" href="javascript:void(0);" onclick="addattr()"><i class="layui-icon">&#xe654;</i>添加属性</a>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <table id="mytable" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs nop" lay-event="detail">查看</a>
    {{# if (d.status=== 1) { }}  
    <a class="layui-btn layui-btn-xs nop" lay-event="edit">编辑</a> 
    <a class="layui-btn layui-btn-xs nop" lay-event="spec">规格</a>
    <a class="layui-btn layui-btn-xs nop" lay-event="status">上架</a>
    {{# } else if(d.status=== 2) { }}  
    <a class="layui-btn layui-btn-xs nop" lay-event="status">下架</a>
    {{# } else { }}  
	{{# } }} 
    <a class="layui-btn layui-btn-danger layui-btn-xs nop" lay-event="del">删除</a>
</script>
<script>
    var list_url=baseUrl+"/goods/list";
    var del_url="/goods/del";
    var status_url="/goods/status"
    var cols=[ //标题栏
        {type:"checkbox",title:'', width: "5%"}
        , {field: 'id', title: 'ID', width: "5%"}
        , {field: 'title', title: '标题', width: "10%"}
        , {field: 'status', title: '状态', width: "10%",templet:function(d){
            if(d.status==1) return "编辑"
            else if(d.status==2) return "发布"
            else return "未知"
        }}
        , {field: 'goods_spec.price', title: '价格', width: "10%",templet:function(d){
            return d.goods_spec[0].price.toFixed(2)
        }}
        , {field: 'goods_spec.num', title: '库存', width: "10%",templet:function(d){
            return d.goods_spec[0].num.toFixed(2)
        }}
        , {field: 'custom', title: '分类', width: "10%",templet:function(d){
            return d.store_custom_class.title
        }}
        , {field: 'right', title: '操作', width: "30%",toolbar: '#barDemo'}
    ]
    var table;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#mytable',
            url: list_url,
            method: 'post',
            headers:{token:sessionStorage.getItem('token')},
            where:{store_id:getQueryString("id")},
            toolbar: false,
            page:true,
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.status.errorCode, //解析接口状态
                    "msg": res.status.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [cols],
            done: function (res, curr, count) {
                $("table").css("width", "100%");
            },
        });

        //监听工具条,,行内
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'detail'){ //查看
                xadmin.open('查看','/goods/detail.html?id='+data.id,600,600)
                //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除么<br/>'+data.title, function(index){
                    u_post(del_url,{id:data.id},function(res){
                        if(res.status.errorCode==0){
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);
                            //table.reload('mytable');
                        }
                        else{
                            layer.msg(res.status.msg)
                        }
                    })
                });
            } else if(layEvent === 'edit'){ //编辑
                xadmin.open('编辑','./edit.html?id='+data.id,450,600)
            } else if(layEvent === 'status'){//发布
                var editstatus=1
                    if(obj.data.status==1){
                       editstatus=2
                    }
                    else if(obj.data.status==2){
                        editstatus=1
                    }
                    u_post(status_url,{id:data.id,status:editstatus},function(res){
                        if(res.status.errorCode==0){
                            table.reload('mytable');
                        }
                        else{
                            layer.msg(res.status.msg)
                        }
                    })
            } else if(layEvent === 'spec'){
                xadmin.open('规格','./spec.html?id='+data.id,600,600)
            } 
        });
    });
    //sreach
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(sreach)', function(data){
            table.reload('mytable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: data.field, //设定异步数据接口的额外参数
                //,height: 300
            });
            return false;
        });
    });
    function adduser(){
        xadmin.open('添加',"./add.html?id=" + getQueryString("id"),450,500)//store_id
    }
    function addattr(){
        data=table.checkStatus("mytable")
        var ids=""
        for(i=0;i<data.data.length;i++){
            ids+=data.data[i].id+","
        }
        if(ids.length==0){
            layer.msg("选择商品",{time:1000})
            return
        }
        xadmin.open('添加属性',"./attribute.html?ids=" + ids,450,500)
    }
</script>
</html>

